#include "Mech.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;
using namespace CocosDenshion;

Mech::Mech() : CCSprite()
{
	moving = false;
	shooting = false;
	hp = 0;
	int _kind;
	_layer = NULL;
	_timeSinceLastShot = 0;
	_turret = NULL;
}

bool Mech::initWithLayer(HelloWorld *layer,int kind,int hp)
{
	CCString *spriteFrameName = CCString::stringWithFormat("tank%d_base.png",kind);  
	if( CCSprite::initWithSpriteFrameName(spriteFrameName->getCString()) )
	{
		moving = false;
		shooting = false;
		_layer = layer;
        _kind = kind;
        this->hp = hp;
		this->scheduleUpdateWithPriority(-1);

		CCString *turretName = CCString::stringWithFormat("tank%d_turret.png",kind);
		_turret = CCSprite::spriteWithSpriteFrameName(turretName->getCString());
		_turret->setAnchorPoint(ccp(0.5, 0.25));
		_turret->setPosition(ccp(this->getContentSize().width/2,this->getContentSize().height/2));
		this->addChild(_turret);
		return true;
	}

    return false;
}

void Mech::moveToward(CCPoint targetPosition)
{    
    _targetPosition = targetPosition;                    
}
 
void Mech::updateMove(float dt)
{
	if (!this->moving) return;

	CCPoint offset = ccpSub(_targetPosition,this->getPosition());
	float MIN_OFFSET = 10;
	if (ccpLength(offset) < MIN_OFFSET) return;

	CCPoint targetVector = ccpNormalize(offset);    
	float POINTS_PER_SECOND = 150;
	CCPoint targetPerSecond = ccpMult(targetVector, POINTS_PER_SECOND);
	CCPoint actualTarget = ccpAdd(this->getPosition(), ccpMult(targetPerSecond, dt));
	CCPoint oldPosition = this->getPosition();
	this->setPosition(actualTarget);

	if( _layer->isWallAtRect(this->boundingBox()) )
	{
		this->setPosition(oldPosition);
	}
}
 
void Mech::update(float dt)
{    
    this->updateMove(dt);
	this->updateShoot(dt);
}

void Mech::shootToward(CCPoint targetPosition)
{
	CCPoint offset = ccpSub(targetPosition,this->getPosition());
	float MIN_OFFSET = 10;
	if (ccpLength(offset) < MIN_OFFSET) return;
 
	_shootVector = ccpNormalize(offset);
}

void Mech::shootNow()
{
	float angle = ccpToAngle(_shootVector);
	_turret->setRotation( (-1 * CC_RADIANS_TO_DEGREES(angle)) + 90 );
	
    float mapMax = max(_layer->tileMapWidth(),_layer->tileMapHeight());
    CCPoint actualVector = ccpMult(_shootVector, mapMax);  
 
    float POINTS_PER_SECOND = 100;
    float duration = mapMax / POINTS_PER_SECOND;
 
    CCString * shootSound = CCString::stringWithFormat("tank%dShoot.wav", _kind);
	SimpleAudioEngine::sharedEngine()->playEffect(shootSound->getCString());
 
    CCString *bulletName = CCString::stringWithFormat("tank%d_bullet.png", _kind);
	CCSprite *bullet = CCSprite::spriteWithSpriteFrameName(bulletName->getCString());
	bullet->setTag(_kind);
	bullet->setPosition(ccpAdd(this->getPosition(), ccpMult(_shootVector, _turret->getContentSize().height)));
    CCMoveBy * move = CCMoveBy::actionWithDuration(duration,actualVector);
	CCCallFuncN * fn = CCCallFuncN::actionWithTarget(this,callfuncN_selector(Mech::destroyBullet));
	bullet->runAction(CCSequence::actions(move,fn,NULL));
	_layer->batchNode->addChild(bullet);
}

void Mech::destroyBullet(CCNode *node)
{
	node->removeFromParentAndCleanup(true);
}

bool Mech::shouldShoot()
{
 
    if (!this->shooting) return false;    
 
    double SECS_BETWEEN_SHOTS = 0.5;

    if (_timeSinceLastShot > SECS_BETWEEN_SHOTS) 
	{        
        _timeSinceLastShot = 0;
        return true;        
    }

	return false;
}
 
void Mech::updateShoot(float dt)
{
    _timeSinceLastShot += dt;
	if(this->shouldShoot()) 
	{
		this->shootNow();       
    }
}